#include "ImageLocation.h"
#include <Uri16.h>
#include "global.h"

void THcImageLocation::ParseImageSrc(const TDesC& aURL)
{
	TUriParser16 parser;
	parser.Parse(aURL);
	
	const TDesC& scheme = parser.Extract(EUriScheme);
	if(scheme.CompareF(_L("file"))==0) 
	{
		const TDesC& path = parser.Extract( EUriPath );
		if(path.Length()>0)
		{
			iFileName.Copy(path.Mid(1));
			if(iFileName.Length()>0)
			{
				UIControlUtil::StringReplace(iFileName, '/', '\\');
				if(iFileName[1]!=':')
					iFileName.Insert(1, _L(":"));
			}
		}
		else
			iFileName.Zero();
	}
	else
		iFileName.Copy(parser.Extract( EUriPath ));

	TPtrC param1, param2, param3, param4;
	const TDesC& frag = parser.Extract(EUriFragment);
	TInt i=0;
	if(frag.Length()>0)
	{
		TInt offset = 0;
		TPtrC p;
		while(UIControlUtil::StringSplit(frag, offset, ',' , p)) 
		{
			if(i==0)
				param1.Set(p);
			else if(i==1)
				param2.Set(p);
			else if(i==2)
				param3.Set(p);
			else if(i==3)
				param4.Set(p);
			else
				break;
			i++;
		}
	}
	
	_LIT(KAppIcon, "appicon");
	_LIT(KGradient, "gradient");
	_LIT(KSkin, "skin");
	_LIT(KIcon, "icon");
	_LIT(KFrame, "frame");
	
	if(iFileName.CompareF(KGradient)==0) 
	{
		iType = ELTGradient;
		iFileName.Zero();
		
		TRgb rgb;
		UIControlUtil::ParseHexColor(param1, rgb);
		iIndex1 = rgb.Value();
		UIControlUtil::ParseHexColor(param2, rgb);
		iIndex2 = rgb.Value();

		if(i>2)
			iIndex3 = UIControlUtil::StrToInt(param3, EHex);
		else
			iIndex3 = 0;
		iValid = ETrue;
	}		
	else
	{
		TPtrC ext;
		TInt pos = iFileName.LocateReverse('.');
		if(pos!=KErrNotFound) 
			ext.Set(iFileName.Mid(pos+1));

		if(ext.CompareF(_L("mif"))==0 || ext.CompareF(_L("svg"))==0)
		{
			iType = ELTFileSvg;
			iIndex1 = 16384;
			iIndex2 = 16385;
			iValid = UIControlEnv::Static()->ResolvePath(iFileName);
			return;
		}
		
		TInt index1 = -1, index2 = -1;
		if(param1.Length()>0)
			index1 = UIControlUtil::StrToInt(param1, EHex);
		if(param2.Length()>0)
			index2 = UIControlUtil::StrToInt(param2, EHex);
		if(param1.Length()==0 && ext.CompareF(_L("mbm"))==0)
		{
			index1 = 0;
			index2 = 1;
		}
		
		if(index1==-1)
		{
			iType = ELTFileGeneral;
			if(iFileName.Length()>0)
				iValid = UIControlEnv::Static()->ResolvePath(iFileName);
			else
				iValid = EFalse;
		}
		else
		{
			iType = ELTFileMbm;
			iIndex1 = index1;
			iIndex2 = index2;
			
			if(iFileName.Length()>0) 
			{				
				if(iFileName[0]!='*')
				{
					iValid = UIControlEnv::Static()->ResolvePath(iFileName);
				}
				else
					iValid = ETrue;
			}
			else
				iValid = EFalse;
		}
	}
}

